package com.googlecode.javatools.classparser.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.googlecode.javatools.classparser.TypesInfoParser;

public class TypesInfoParserImpl implements TypesInfoParser
{
	private static final char CLASS_DESCRIPTOR = 'L';
	private static final char END_OF_TYPE_IDENTIFIER = ';';

	public Collection<String> parse(final String descriptor)
	{
		final List<String> types = new ArrayList<String>();
		for (int index = 0; index < descriptor.length(); index++)
		{
			final int startIndex = descriptor.indexOf(CLASS_DESCRIPTOR, index);
			if (startIndex >= 0)
			{
				index = descriptor.indexOf(END_OF_TYPE_IDENTIFIER, startIndex + 1);
				types.add(descriptor.substring(startIndex + 1, index));
			}
		}

		return slashesToDots(types);
	}

	private Collection<String> slashesToDots(final List<String> types)
	{
		final Collection<String> qualifiedTypes = new ArrayList<String>();
		for (final String s : types)
		{
			qualifiedTypes.add(s.replace('/', '.'));
		}
		return qualifiedTypes;
	}
}
